package SmallChange.OOP;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

/**
 * @Package: SmallChange.OOP
 * @ClassName: SmallChangeSysOOP
 * @Author: x2606
 * @CreateTime: 2022/3/4 15:31
 * @Description:
 */

public class SmallChangeSysOOP {
    //菜单显示循环变量
    Boolean loop = true;

    //接受输入选项
    Scanner scanner=new Scanner(System.in);
    String key="";

    //根据需求定义变量

    //使用字符串拼接来实现明细
    String details="-----------零钱明细-----------";

    double money=0;
    double balance=0;

    //java.util.Date表示日期
    Date date=null;
    //java.text.SimpleDateFormat 用于处理时间格式
    SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH-mm");

    String name=null;

    String shutdown="";

    //显示菜单
    public void menu(){
        do{
            System.out.println("\n============零钱通系统(oop)============");
            System.out.println("\t\t\t1.零钱通明细");
            System.out.println("\t\t\t2.存     入");
            System.out.println("\t\t\t3.消     费");
            System.out.println("\t\t\t4.退     出");
            System.out.print("请输入选项（1-4）：");
            key=scanner.next();

            switch (key){
                case "1":
                    this.detail();
                    break;
                case "2":
                    this.income();
                    break;
                case "3":
                    this.expend();
                    break;
                case "4":
                    this.sysOut();
                    break;
                default:
                    System.out.println("输入的选项错误，请重新输入：");
            }
        }while (loop);
    }

    //显示明细
    public void detail(){
        System.out.println(details);
    }

    //收益入账
    public void income(){
        System.out.print("收益入账金额：");
        money=scanner.nextDouble();
        if (money<=0){
            System.out.println("收入金额需要大于0！");
            return;
        }
        balance+=money;

        //获取当前时间
        date=new Date();
        details+="\n收益\t+"+money+"\t"+sdf.format(date)+"\t余额："+balance;
        return;
    }

    //消费
    public void expend(){
        System.out.print("消费名称：");
        name=scanner.next();
        System.out.print("消费金额：");
        money=scanner.nextDouble();
        if (money>balance||money<=0){
            System.out.println("消费金额需要大于0或者小于等于余额！");
            return;
        }
        balance-=money;
        date=new Date();
        details+="\n"+name+"\t-"+money+"\t"+sdf.format(date)+"\t余额："+balance;
        return;
    }

    //退出
    public void sysOut(){
        do{
            System.out.print("你确定要退出吗？y/n:");
            shutdown=scanner.next();
            if (shutdown.equals("y")){
                loop=false;
                return;
            }else if(shutdown.equals("n")) {
                return;
            }else {
                System.out.println("输入错误，请重新输入！");
            }
        }while (true);
    }



}
